#!/usr/bin/python3
# -*-coding:utf-8 -*-
# @Author  : caoyn
# @Time    : 2021/2/02 12:30 下午
# @Software: PyCharm


import requests
from utils import timeutils
from url import goodsissue_url
import pytest


"""
第二周作业：好生意-销货单选择一个商品，保存销货单，加断言；
第三周作业：
a、销货单保存、加入动态获取token 动态获取code 动态获取时间戳
b、查看销货单详情接口
c、都加入断言

"""

# def get_token(ua, pw, dm, ev):
#     """
#     获取token
#     :param ua: 18701572385
#     :param pw: nan123456
#     :param dm: u31tdnagdkg7/mxkx5ur9gc
#     :param ev: online
#     :return:
#     """
#     url = 'http://172.18.22.177:8000/login?username=%s&password=%s&domain=%s&env_name=%s' % (ua, pw, dm, ev)
#     return requests.get(url)


# 新增销货单
def test_creat_Goodsissue():
    headers = {
        "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3MDAzZjZlMC1mZWYxLTQ5NTgtOWFjMy02OTg1NTdjMWRlMGUiLCJpYXQiOjE2MTQyMTY5ODZ9.eIMX07KSXB2yY8dkKAj5qGbyD3Vz5O3sApTSqAYfhi0",
        "content-type": "application/json; charset=utf-8",
        "Connection": "keep-alive",
    }

    # 获取销货单code、txnToken
    url1 = goodsissue_url.goodsissue_url.Get_initData_url
    response = requests.get(url=url1, headers=headers)
    result = response.json()
    code = result["data"]["code"]
    txnToken = result["data"]["txnToken"]

    print('销货单code：', code)
    print('txnToken：', txnToken)

    # 发送一个新增销货单的请求
    url2 = goodsissue_url.goodsissue_url.Creat_GoodsIssue_url

    goodsItem = {

        "sequenceNum": 0,
        "productId": 612262361563144,
        "refBoName": None,
        "refDetailBoName":  None,
        "refVoucherId": None,
        "refVoucherDetailId":  None,
        "refVoucherCode":  None,
        "srcSalesOrderId":  None,
        "srcSalesOrderCode":  None,
        "refNetDiscount": -468,
        "warehouseId": 1073531654242311,
        "manualInputFlag": 536881153,
        "netManualInputFlag": 536881153,
        "transUomId": 564348915417260,
        "itemBarcode": "",
        "baseUomId": 564348915417260,
        "taxPct": 0.17,
        "stdPriceFlagEnum": "PRICE_WITHOUT_TAX",
        "discountPct": 5,
        "netDiscountPct": 5,
        "isFreeGift": False,
        "isListPriceIncludingTax": False,
        "transToBaseRate": 1,
        "isTransToBaseFloating": False,
        "listPrice": 100,
        "priceWithoutTax": 500,
        "priceTypeId": None,
        "trans2ToTransRate": None,
        "trans2ToBaseRate": None,
        "trans2ToAuxRate": None,
        "transToAuxRate": None,
        "auxToBaseRate": None,
        "priceWithTax": 585,
        "tax": 85,
        "amountWithTax": 585,
        "amountWithoutTax": 500,
        "discount": -468,
        "listPriceWithoutPromo": 100,
        "netPriceWithTax": 585,
        "netPriceWithoutTax": 500,
        "netTax": 85,
        "netAmountWithTax": 585,
        "netAmountWithoutTax": 500,
        "netDiscount": -468,
        "netStdPriceFlagEnum": "PRICE_WITHOUT_TAX",
        "detailPromoList": [
        ],
        "promoName": "",
        "promoUsedStatusEnum": "NO_PROMO_OR_MATCH_BUT_NOT_USE",
        "promoDiscountForProduct": 0,
        "costPrice": 62.0204,
        "costAmount": 62.02,
        "quoteAmount": 100,
        "transQty": 1,
        "baseQty": 1,
        "hierarchyPkgQtys": {
        },
        "hierarchyPkgQtysText": "1包",
        "extraDiscount": 0,
        "promoDiscount": 0,
        "productSpecId": None,
        "uuid": "21",
        "editFlag": "new"
    }
    #创建一个列表
    goodsItems = []

    goodsItems.append(goodsItem)

    data = {

        "data": {
            "bizDate": timeutils.get_current_timestamp(),
            "code": code,
            "redBlueFlagEnum": "BLUE",
            "promoList": [
            ],
            "goodsItems": goodsItems,
            "paymentList": [
            ],
            "goodsIssueExpenseList": [
            ],
            "paymentExpenseList": [
            ],
            "attachmentList": [
            ],
            "paymentFreightList": [
            ],
            "isPriceTracking": True,
            "bizTypeId": 100011,
            "expenseNoteTypeEnum": "NO_INVOICE",
            "txnToken": txnToken,
            "noteTypeEnum": "NORMORL_INVOICE",
            "exchangeRate": 1,
            "expenseAllocationTypeEnum": "ALLOCATED_BY_AMOUNT",
            "paymentCollectMethodEnum": "ON_CREDIT",
            "warehouseId": 1073531654242311,
            "totalPromoDiscount": 0,
            "totalNetAmountWithTax": 585,
            "totalAmountWithTax": 585,
            "dueDate": timeutils.get_current_timestamp(),
            "soldToCustId": 565473659977729,
            "billToCustId": 565473659977729,
            "departmentId": 601357171425280,
            "bizEmployeeId": 564348914630657,
            "paymentTermsEnum": "MONTHLY_PAYMENT",
            "refBoName": None,
            "refVoucherCode": None,
            "srcSalesOrderId": None,
            "pickupTruckNo": None,
            "isStockOutImmediate": None,
            "pickupPersonName": None,
            "pickupDriverName": None,
            "pickupCarrierId": None,
            "pickupTrackingNo": None,
            "pickupComments": None,
            "shipMethodEnum": "OTHER",
            "shipToAttnName": "曹亚南",
            "shipToAddress1": "",
            "shipToTelephoneNum": "18701572385",
            "shipToProvince": "",
            "shipToCity": "",
            "shipToDisctrict": "",
            "shipToGeoCode": "",
            "custVendorContactId": 1003641671254018,
            "masterToDetailAssocList": [
            ],
            "isCustChargedFee": None,
            "voucherEntrySourceEnum": "NORMAL",
            "productSpecId": None
        },
        "params": {
        }
    }

    response = requests.post(url=url2, json=data, headers=headers)
    assert response.status_code == 200
    result = response.json()
    id = result["id"]
    print('响应结果：', response.text)
    print('销货单id：', id)

    # 销货单查看态数据校验

    url3 = "https://cloud.chanjet.com/hsy/u31tdnagdkg7/mxkx5ur9gc/voucher/GoodsIssue/pick?id=%s" % id
    response = requests.get(url=url3, headers=headers)
    assert response.status_code == 200
    result = response.json()
    print('响应结果：', response.text)
    totalNetAmountWithTax = result["data"]["totalNetAmountWithTax"]
    totalAmountWithoutTax = result["data"]["totalAmountWithoutTax"]

    # totalNetAmountWithTax 成交金额
    # totalAmountWithoutTax 优惠前无税总金额
    assert totalNetAmountWithTax == 585
    assert totalAmountWithoutTax == 500


# 调用pytest的main函数执行测试
if __name__ == '__main__':
    pytest.main('GoodsIssue.py')


